<?php

namespace app\common\library;


use app\admin\model\Setting;
use fast\Http;
use think\Env;
use think\Exception;

// 如果一次团的是多张，扫大众点评二维码得到的，其实就是第一张的团券code，其实扫码验券的接口没有什么用
// 验券接口还是一张一张的核销

// 在application/index/controller/Index.php文件里看到怎么得到
// auth_code 和 open_shop_uuid
// auth_code 每一年要更新一次

class ThirdDianping
{
    const URL = 'http://wspacebot.yunfeiwork.com';
    const APP_ID = 'e2713c2a64125d7e5964a39c1b4b9140';
    const APP_SECRET = '46ce8d0999b8b7959488a991d339b84f';
    const OP_POIID = '18182';

    public function __construct() {}

    public function exchange($code, $count = 1)
    {
        $session = $this->_getSession();
        if (false == $session) {
            throw new Exception('session没有获得');
        }

        $headers = ['content-type:application/json'];
        $options = [
            CURLOPT_HTTPHEADER => $headers
        ];

        $url = self::URL . '/wspace-openapi/partner/tuangou/receipt/consume?access_token=' . $session;
        $data = [
            'receiptCode' => $code,
            'opPoiId' => self::OP_POIID,
            'platform' => 'meituan',
            'type' => 2
        ];
        $result = Http::post($url, json_encode($data), $options);
        $result = json_decode($result, true);
        return $result;
    }

    public function prepare($code)
    {
        $session = $this->_getSession();
        if (false == $session) {
            throw new Exception('session没有获得');
        }

        $headers = ['content-type:application/json'];
        $options = [
            CURLOPT_HTTPHEADER => $headers
        ];
        $url = self::URL . '/wspace-openapi/partner/tuangou/receipt/consume?access_token=' . $session;
        $data = [
            'receiptCode' => $code,
            'opPoiId' => self::OP_POIID,
            'platform' => 'meituan',
            'type' => 1
        ];
        $result = Http::post($url, json_encode($data), $options);
        $result = json_decode($result, true);
        return $result;
    }

    protected function _getSession()
    {
        $dianpingSession = Setting::where('name', 'third_dianping_session')->find();
        $dianpingExpiresIn = Setting::where('name', 'third_dianping_expires_in')->find();

        $headers = ['content-type:application/json'];
        $options = [
            CURLOPT_HTTPHEADER => $headers
        ];

        // 第一次得到session
        if (
            empty($dianpingSession->value)
            || $dianpingSession->updatetime + $dianpingExpiresIn->value < time() + 3600 * 24 * 3
        ) {
            $url = self::URL . '/wspace-openapi/oauth/token';
            $data = [
                'appId' => self::APP_ID,
                'appSecret' => self::APP_SECRET,
            ];
            $result = Http::post($url, json_encode($data), $options);
            $result = json_decode($result, true);
            if (200 == $result['code']) {
                $dianpingSession->value = $result['data']['accessToken'];
                $dianpingExpiresIn->value = $result['data']['expiresIn'];

                $dianpingSession->save();
                $dianpingExpiresIn->save();
            } else {
                return false;
            }
        }
        return $dianpingSession->value;
    }
}
